#include "qsetborderdialog.h"
#include "ui_qsetborderdialog.h"

#include <QStringList>
#include <QMouseEvent>
#include <QColorDialog>

QSetBorderDialog::QSetBorderDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::QSetBorderDialog),
    m_ret(0)
{
    ui->setupUi(this);

    QStringList list;

    list<<"none"<<"solid"<<"ridge"<<"outset"<<"inset"<<"groove"<<"double"<<"dotted"
       <<"dot-dot-dash"<<"dot-dash"<<"dashed";
    ui->comboBox->addItems(list);
    ui->comboBox->setCurrentIndex(0);
    updateStyleSheet();
    ui->widget_2->installEventFilter(this);
}

QSetBorderDialog::~QSetBorderDialog()
{
    delete ui;
}

void QSetBorderDialog::on_okBtn_clicked()
{
    m_ret=1;
    close();
}

void QSetBorderDialog::on_cancelBtn_clicked()
{
    m_ret=0;
    close();
}

void QSetBorderDialog::setBorderInfo(tagBorderInfo info)
{
    QString str;
    int index=ui->comboBox->findText(info.m_border_type);
    if(index<0)
    {
        index=0;
    }
    ui->comboBox->setCurrentIndex(index);
    ui->spinBox->setValue(info.m_border_width);
    m_color=info.m_color;
    str=QString("background-color: rgba(%1, %2, %3, %4);").arg(info.m_color.red())
            .arg(info.m_color.green()).arg(info.m_color.blue()).arg(info.m_color.alpha());
    ui->widget->setStyleSheet(str);
    updateStyleSheet();
    connect(ui->comboBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(updateStyleSheet()));
    connect(ui->spinBox,SIGNAL(valueChanged(int)),this,SLOT(updateStyleSheet()));
}

void QSetBorderDialog::updateStyleSheet()
{
    QString str;

    str=QString("border:%1px %2 rgba(%3,%4,%5,%6)").arg(ui->spinBox->value()).arg(ui->comboBox->currentText())
            .arg(m_color.red()).arg(m_color.green()).arg(m_color.blue()).arg(m_color.alpha());
    qDebug(str.toLocal8Bit());
    ui->widget->setStyleSheet(str);
}

tagBorderInfo QSetBorderDialog::getBorderInfo()
{
    tagBorderInfo info;
    info.m_isNull=false;
    info.m_border_type=ui->comboBox->currentText();
    info.m_color=m_color;
    info.m_border_width=ui->spinBox->value();
    return info;
}

bool QSetBorderDialog::eventFilter(QObject *o, QEvent *e)
{
    if(o==ui->widget_2)
    {
        if(e->type()==QEvent::MouseButtonRelease)
        {
            QMouseEvent *event=(QMouseEvent*)e;
            if(event->button()==Qt::LeftButton)
            {
                QColor c=QColorDialog::getColor(m_color,this,tr("Get border color"),QColorDialog::ShowAlphaChannel);
                if(c.isValid())
                {
                    m_color=c;
                    QString str;
                    str=QString("background-color: rgba(%1, %2, %3, %4);").arg(m_color.red())
                            .arg(m_color.green()).arg(m_color.blue()).arg(m_color.alpha());
                    ui->widget_2->setStyleSheet(str);
                    updateStyleSheet();
                }
                return true;
            }
        }
    }
    return QWidget::eventFilter(o,e);
}

int QSetBorderDialog::getRet()
{
    return m_ret;
}
